package com.recommender;

import java.sql.ResultSet;
import java.util.*;

import textanalyzer.MainAnalyzer;

import Neighborhood.Neighborhood;

import com.model.DataModel;
import com.model.TagBasedDataModel;
import com.model.TagBasedUserDataModel;
import com.similarity.ContextSimilarity;
import com.similarity.TagBasedUserSimilarity;
import com.similarity.TagSimilarity;
import com.similarity.UserSimilarity;

public class TagBasedRecommender implements Recommender {
	ContextSimilarity tSim;
	TagBasedDataModel tagBasedModel;
	Neighborhood neighborhood;

	@Override
	public Map<Integer, Double> getRecommendations(
			HashMap<String, String> contextMap, String keyword, long userID,
			DataModel datamodel, Neighborhood neighborhood) {

		ResultSet tuples = null;
		tagBasedModel = (TagBasedDataModel) datamodel;
		this.neighborhood=neighborhood;
		String tagSetString = contextMap.get("tags");
		System.out.println(tagSetString);

		HashMap<Integer, Double> myHashMap = new HashMap<Integer, Double>();
		Map<Integer, Double> imageResultsMap = new HashMap<Integer, Double>();

		try {
			tuples = tagBasedModel.getTuplesFilteredByKeyword(keyword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int tmimageID = -1;

		Map<String, Integer> tagVector = new LinkedHashMap<String, Integer>();
		MainAnalyzer nAnalyzer = new MainAnalyzer();
		tagVector = nAnalyzer.identifyContext(tagSetString);

		try {

			while (tuples.next()) {
				double predictedTagSimilarity = 0.0f;
				int imageID = tuples.getInt("ImageID");
				if (imageID == tmimageID)
					continue;
				tmimageID = imageID;

				TagSimilarity tagSimilarity = new TagSimilarity(tagBasedModel);
				predictedTagSimilarity = tagSimilarity.calculateSimilarity(
						tagVector, (long) imageID);
				
				System.out.println("predictedTagSimilarity for image "
						+ imageID + " " + predictedTagSimilarity);
				
				if (predictedTagSimilarity == 0)
					continue;
				myHashMap.put(imageID, predictedTagSimilarity);

				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		imageResultsMap=neighborhood.getFilteredImages(myHashMap);
		
		return imageResultsMap;
		}

	

}
